package edu.bupt.changelog;

import edu.bupt.changelog.demo.service.AgentService;
import edu.bupt.changelog.demo.service.RequestService;
import edu.bupt.changelog.demo.service.dto.AddRequestRequest;
import edu.bupt.changelog.demo.service.dto.QueryAgentRequest;
import edu.bupt.changelog.demo.service.dto.RequestDO;
import edu.bupt.changelog.demo.service.mapper.LogAgentService;
import edu.bupt.changelog.history.defaultsaver.DefaultChangeSaver;
import org.junit.Assert;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Optional;


@SpringBootApplication
public class LearnApp {
    public static void main(String[] args) {

        ConfigurableApplicationContext app = SpringApplication.run(LearnApp.class);

        LogAgentService logAgentService = app.getBean(LogAgentService.class);
        QueryAgentRequest queryAgentRequest = new QueryAgentRequest();
        queryAgentRequest.setAgentId(1L);
        logAgentService.insert(queryAgentRequest);


        List<List<Object>> historyStore = DefaultChangeSaver.store;
        Map requestStore = RequestService.map;
        Map agentStore = AgentService.map;

        RequestService bean = app.getBean(RequestService.class);
        Assert.assertEquals(0,requestStore.size());

        bean.put(buildRequest(1L,"1","yi"));
        Assert.assertEquals(1,requestStore.size());
        Assert.assertEquals(1L,bean.get(1L).getRequestId().longValue());
        Assert.assertEquals(1,historyStore.size());
        Assert.assertEquals("1",historyStore.get(historyStore.size()-1).get(0));
        Assert.assertEquals("yi",((Optional<RequestDO>)historyStore.get(historyStore.size()-1).get(2)).get().getAddress());


        bean.put(buildRequest(2L,"2","er"));
        Assert.assertEquals(2,requestStore.size());
        Assert.assertEquals("er",bean.get(2L).getAddress());
        Assert.assertEquals(2,historyStore.size());


        bean.put(buildRequest(1L,"1new","yinew"));
        Assert.assertEquals(2,requestStore.size());
        Assert.assertEquals("1new",bean.get(1L).getName());
        Assert.assertEquals(3,historyStore.size());

        bean.del(1L);
        Assert.assertEquals(1,requestStore.size());
        Assert.assertNull(bean.get(1L));
        Assert.assertEquals(4,historyStore.size());


        AgentService agentService = app.getBean(AgentService.class);
        agentService.add(buildAgentRequest(1,"name1","address1"));
        Assert.assertEquals(1,agentStore.size());
        Assert.assertEquals("name1",agentService.query(buildAgentRequest(1L,"","")).getAgentName());
        Assert.assertEquals(5,historyStore.size());


        agentService.add(buildAgentRequest(2,"name2","address2"));
        Assert.assertEquals(2,agentStore.size());
        Assert.assertEquals("name2",agentService.query(buildAgentRequest(2L,"","")).getAgentName());
        Assert.assertEquals(6,historyStore.size());


        agentService.update(buildAgentRequest(1,"name1new","address1new"));
        Assert.assertEquals(2,agentStore.size());
        Assert.assertEquals("name1new",agentService.query(buildAgentRequest(1L,"","")).getAgentName());
        Assert.assertEquals(7,historyStore.size());

        agentService.del(buildAgentRequest(2,"",""));
        Assert.assertEquals(1,agentStore.size());
        Assert.assertNull(agentService.query(buildAgentRequest(2L,"","")));
        Assert.assertEquals(8,historyStore.size());

    }

    public static QueryAgentRequest buildAgentRequest(long id, String name, String manager){
        QueryAgentRequest req = new QueryAgentRequest();
        req.setAgentId(id);
        req.setName(name);
        req.setManager(manager);
        return req;
    }

    public static AddRequestRequest buildRequest(long id, String name, String address){
        AddRequestRequest addRequestRequest = new AddRequestRequest();

        addRequestRequest.setRequestId(id);
        addRequestRequest.setName(name);
        addRequestRequest.setAddress(address);
        return addRequestRequest;
    }

}
